﻿using DiffuseEnjoyIdle.Entity.Entitys;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Text;

namespace DiffuseEnjoyIdle.EntityFrameworkCore
{
    public class DBContext : DbContext
    {

        public DBContext(DbContextOptions<DBContext> options) : base(options)
        {

        }

        /// <summary>
        /// 用户
        /// </summary>
        public DbSet<Users> Users { set; get; }
        /// <summary>
        /// 评论
        /// </summary>
        public DbSet<Comments> Comments { set; get; }
        /// <summary>
        /// 商品
        /// </summary>
        public DbSet<Goods> Goods { set; get; }
        /// <summary>
        /// 商品评论
        /// </summary>
        public DbSet<GoodsComments> GoodsComments { set; get; }
        /// <summary>
        /// 身份绑定
        /// </summary>
        public DbSet<IdentityBindings> IdentityBindings { set; get; }
        /// <summary>
        ///收货地址
        /// </summary>
        public DbSet<MailingAddress> MailingAddresses { set; get; }
        /// <summary>
        /// 订单
        /// </summary>
        public DbSet<Order> Orders { set; get; }

        private static void TableName<TEntity>(EntityTypeBuilder<TEntity> entity)
       where TEntity : class
        {
            const string prefix = "de";
            var name = typeof(TEntity).Name;
            entity.ToTable(GenName(prefix, name));
        }
        private static string GenName(string prefix, string name)
        {
            var sb = new StringBuilder(100);
            sb.Append(prefix);

            foreach (var ch in name)
            {
                if (ch >= 'A' && ch <= 'Z')
                {
                    sb.Append('_');
                    sb.Append((char)(ch + 32));
                }
                else
                    sb.Append(ch);
            }


            return sb.ToString();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            modelBuilder.Entity<Users>(entity =>
            {
                TableName(entity);
                entity.HasKey(a => a.Id);
                entity.Property(a => a.Id).ValueGeneratedNever();//值不自动生成
            });

            modelBuilder.Entity<Goods>(entity =>
            {
                TableName(entity);
                entity.HasKey(a => a.Id);
                entity.Property(a => a.Id).ValueGeneratedNever();//值不自动生成
                entity.Property(a => a.Price).HasPrecision(18, 2);
                entity.Property(a => a.OriginalPrice).HasPrecision(18, 2);
                entity.Property(a => a.Freight).HasPrecision(18, 2);
            });
            modelBuilder.Entity<Order>(entity =>
            {
                TableName(entity);
                entity.HasKey(a => a.Id);
                entity.Property(a => a.Id).ValueGeneratedNever();//值不自动生成

            });

            modelBuilder.Entity<Comments>(entity =>
            {
                TableName(entity);
                entity.HasKey(a => a.Id);
                entity.Property(a => a.Id).ValueGeneratedNever();//值不自动生成

            });
            modelBuilder.Entity<GoodsComments>(entity =>
            {
                TableName(entity);
                entity.HasKey(a => a.Id);
                entity.Property(a => a.Id).ValueGeneratedNever();//值不自动生成

            });
            modelBuilder.Entity<IdentityBindings>(entity =>
            {
                TableName(entity);
                entity.HasKey(a => a.Id);
                entity.Property(a => a.Id).ValueGeneratedNever();//值不自动生成

            });
            modelBuilder.Entity<MailingAddress>(entity =>
            {
                TableName(entity);
                entity.HasKey(a => a.Id);
                entity.Property(a => a.Id).ValueGeneratedNever();//值不自动生成

            });


        }
    }
}
